package com.jhy.review.linkedlist;

import java.util.Stack;

/**
 * 是否是回文链表  栈的方法
 */
public class PalindromicLinkedList {

    public static  boolean IsPalindromic(Node node){
        boolean flag = false;
        Node temp = node;
        if(node == null){
            return  false;
        }
        Stack<Node> nodes = new Stack<>();
        while(node != null) {

            nodes.push(node);
            node = node.next;
        }
        while(temp != null) {
            Node pop = nodes.pop();
            if(pop.val != temp.val){
                break;
            }
            temp = temp.next;
            flag = true;
        }
        return flag;
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        Node node1 = new Node(2);
        Node node2 = new Node(3);
        Node node3 = new Node(1);
        Node node4 = new Node(1);
        head.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        boolean b = IsPalindromic(head);
        System.out.println(b);

    }

}

class Node {
    int val;    //数值 data
    Node next;    // 结点 node

    Node(int x) {    //可以定义一个有参构造方法，也可以定义一个无参构造方法
        val = x;
    }
}
